﻿namespace KeRui.BLL
{
    using KeRui.Common;
    using KeRui.DAL;
    using KeRui.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Text.RegularExpressions;

    public class CmsCatchParseRegex
    {
        private readonly KeRui.DAL.CmsCatchParseRegex dal = new KeRui.DAL.CmsCatchParseRegex();

        public int Add(KeRui.Model.CmsCatchParseRegex model)
        {
            return this.dal.Add(model);
        }

        public List<KeRui.Model.CmsCatchParseRegex> DataTableToList(DataTable dt)
        {
            List<KeRui.Model.CmsCatchParseRegex> list = new List<KeRui.Model.CmsCatchParseRegex>();
            int count = dt.Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    KeRui.Model.CmsCatchParseRegex item = new KeRui.Model.CmsCatchParseRegex();
                    if (dt.Rows[i]["ID"].ToString() != "")
                    {
                        item.ID = int.Parse(dt.Rows[i]["ID"].ToString());
                    }
                    if (dt.Rows[i]["ParseID"].ToString() != "")
                    {
                        item.ParseID = int.Parse(dt.Rows[i]["ParseID"].ToString());
                    }
                    item.FieldName = dt.Rows[i]["FieldName"].ToString();
                    item.Pattern = dt.Rows[i]["Pattern"].ToString();
                    item.Replacement = dt.Rows[i]["Replacement"].ToString();
                    if (dt.Rows[i]["CreateTime"].ToString() != "")
                    {
                        item.CreateTime = DateTime.Parse(dt.Rows[i]["CreateTime"].ToString());
                    }
                    list.Add(item);
                }
            }
            return list;
        }

        public void Delete(int ID)
        {
            this.dal.Delete(ID);
        }

        public List<KeRui.Model.CmsCatchParseRegex> GetList(int ParseID)
        {
            DataSet set = new KeRui.DAL.Common().GetList(" CmsCatchParseRegex ", -1, -1, " ParseID=" + ParseID + "", " FieldName desc");
            return this.DataTableToList(set.Tables[0]);
        }

        public List<KeRui.Model.CmsCatchParseRegex> GetListByCache(int ParseID)
        {
            string cacheKey = "CmsCatchParseRegexList-" + ParseID;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.GetList(ParseID);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("ModelCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (List<KeRui.Model.CmsCatchParseRegex>) cache;
        }

        public KeRui.Model.CmsCatchParseRegex GetModel(int ID)
        {
            return this.dal.GetModel(ID);
        }

        public KeRui.Model.CmsCatchParseRegex GetModelByCache(int ID)
        {
            string cacheKey = "CmsCatchParseRegexModel-" + ID;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.dal.GetModel(ID);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("ModelCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (KeRui.Model.CmsCatchParseRegex) cache;
        }

        public string GetRegexFiled(string RegexStr)
        {
            string str = "";
            RegexOptions options = RegexOptions.Multiline | RegexOptions.IgnoreCase;
            MatchCollection matchs = Regex.Matches(RegexStr, @"\(\?\<(?<fieldname>[0-9a-zA-Z]*?)\>(?<pattern>[\s\S]*?)\)", options);
            int num = 0;
            foreach (Match match in matchs)
            {
                string str2 = match.Groups["fieldname"].Value.Trim().ToLower();
                if (str2 != "")
                {
                    if (num != 0)
                    {
                        str = str + ",";
                    }
                    str = str + str2;
                    num++;
                }
            }
            return str;
        }

        public string[] GetRegexFiledArray(string RegexStr)
        {
            return Utils.SplitString(this.GetRegexFiled(RegexStr), ",");
        }

        public void Update(KeRui.Model.CmsCatchParseRegex model)
        {
            this.dal.Update(model);
        }
    }
}

